package cn.tangwei.user.service.impl;

import cn.tangwei.basic.exception.BusinessException;
import cn.tangwei.basic.service.impl.BaseServiceImpl;
import cn.tangwei.basic.utils.MD5Utils;
import cn.tangwei.basic.utils.StrUtils;
import cn.tangwei.org.domain.Employee;
import cn.tangwei.org.domain.Shop;
import cn.tangwei.org.mapper.EmployeeMapper;
import cn.tangwei.org.mapper.ShopMapper;
import cn.tangwei.org.service.IShopService;
import cn.tangwei.user.constants.UserConstants;
import cn.tangwei.user.domain.LoginInfo;
import cn.tangwei.user.domain.User;
import cn.tangwei.user.dto.UserDto;
import cn.tangwei.user.mapper.LoginInfoMapper;
import cn.tangwei.user.mapper.UserMapper;
import cn.tangwei.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    // 基本的CRUD 分页加高级查询 批量删除通过继承BaseServiceImpl得到
    // 自己写的自定义方法不能抽取
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Override
    public void add(User user) {
        LoginInfo loginInfo = user2Loginfo(user);
        loginInfoMapper.save(loginInfo);
        userMapper.save(user);
    }

    @Override
    public void update(User user) {
        LoginInfo loginInfo = user2Loginfo(user);
        loginInfoMapper.update(loginInfo);
        userMapper.update(user);
    }

    @Override
    public void delete(Long id) {
        User user = userMapper.queryById(id);
        loginInfoMapper.remove(user.getLogininfo_id());
        userMapper.remove(id);
    }

    @Override
    public void register(UserDto userDto) {
        // 校验
        // 1.空校验
        if(StringUtils.isEmpty(userDto.getPhone()) || StringUtils.isEmpty(userDto.getVerifyCode())||
                StringUtils.isEmpty(userDto.getPassword()) || StringUtils.isEmpty(userDto.getConfirmPwd())){
            throw new BusinessException("选项不能为空");
        }
        // 2.两次输入密码不一致
        if(!userDto.getPassword().equals(userDto.getConfirmPwd())){
            throw new BusinessException("两次密码输入不一致");
        }
        // 3.手机号已经被注册
        User phone = userMapper.loadByPhone(userDto.getPhone());
        if(phone != null){
            throw new BusinessException("该手机号已经被注册");
        }
        // 4.验证是否通过
        Object codeTmp = redisTemplate.opsForValue().get(UserConstants.BUSINESS_REGISTER_PREFIX + userDto.getPhone());
        if(codeTmp == null){
            throw new BusinessException("验证码失效了，请重新获取验证码");
        }
        // 5.验证码是否相等
        String code = codeTmp.toString().split(":")[0];
        if (!userDto.getVerifyCode().equalsIgnoreCase(code)){
            throw new BusinessException("验证码错误");
        }
        // 保存数据
        User user = userDto2User(userDto);
        LoginInfo loginInfo = user2Loginfo(user);
        // 保存logininfo
        loginInfoMapper.save(loginInfo);
        // 保存user
        user.setLogininfo_id(loginInfo.getId());
        userMapper.save(user);
    }

    private LoginInfo user2Loginfo(User user) {
        LoginInfo loginInfo = new LoginInfo();
        // 属性同名原则拷贝
        BeanUtils.copyProperties(user, loginInfo);
        loginInfo.setType(0);
        return loginInfo;
    }

    private User userDto2User(UserDto userDto) {
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        // 获取盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 使用盐值加密之后的密码
        String md5Tmp = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Tmp);
        return user;
    }
}
